#include <iostream>
#include <algorithm>
#include <string>
#include <cstdio>
#include <vector>
#include <stack>
#include <queue>
#include <set>
using namespace std;
struct ListNode
{
    int val;
    ListNode *next;
    ListNode(int x) : val(x), next(NULL) {}
};
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution
{
public:
    ListNode *addTwoNumbers(ListNode *l1, ListNode *l2)
    {
        int add = 0;
        ListNode *head = new ListNode(0);
        ListNode *curr = head;
        while ((l1 != NULL) || (l2 != NULL))
        {
            int val1 = 0;
            int val2 = 0;
            if (l1 != NULL)
            {
                val1 = l1->val;
                l1 = l1->next;
            }
            if (l2 != NULL)
            {
                val2 = l2->val;
                l2 = l2->next;
            }

            int value = val1 + val2 + add;
            int temp = value % 10;
            add = value / 10;

            curr->next = new ListNode(temp);
            curr = curr->next;
        }
        if (add > 0)
        {
            curr->next = new ListNode(add);
        }
        return head->next;
    }
};